package tools.shapes;

import java.awt.Point;
import java.awt.Graphics;
import java.awt.Color;
import app.DrawingCanvas;

/**
 * This class contains the functionality and parameters common to all types of shapes.
 *
 */
public abstract class Shape {

	protected int xMin;
	protected int yMin;
	protected int xMax;
	protected int yMax;
	protected Color color;

	abstract public void draw(DrawingCanvas c);
	abstract public void drawOutline(DrawingCanvas c);
	abstract public void resize(Point startPos, Point newPos);
	abstract public void move(Point startPos, Point newPos);
	
	public void drawBoundingBox(Graphics g) {
		Color savedColor = g.getColor();
		g.setXORMode(Color.LIGHT_GRAY);
		g.setColor(Color.WHITE);
		
		int x0 = Math.min(xMin, xMax);
		int x1 = Math.max(xMin, xMax);
		int y0 = Math.min(yMin, yMax);
		int y1 = Math.max(yMin, yMax);
		
		if(Math.abs(x0-x1) <= 3){
			x0 = x0 - 3;
			x1 = x1 + 3;
		}
		
		if(Math.abs(y0-y1) <= 3){
			y0 = y0 - 3;
			y1 = y1 + 3;
		}	
		
		g.drawRect(x0, y0,
				x1-x0, y1-y0);
		g.fillRect(x0 - 3, y0 - 3, 7, 7);
		g.fillRect(x0 - 3, y1 - 3, 7, 7);
		g.fillRect(x1 - 3, y0 - 3, 7, 7);
		g.fillRect(x1 - 3, y1 - 3, 7, 7);
		
		g.setColor(savedColor);
		g.setPaintMode();
	}
	
	public boolean insideBoundingBox(Point p) {
		
		int x0 = Math.min(xMin, xMax);
		int x1 = Math.max(xMin, xMax);
		int y0 = Math.min(yMin, yMax);
		int y1 = Math.max(yMin, yMax);
		
		/* modification if the x difference is too small */
		if( Math.abs(xMin - xMax) <= 3 ){
			x0 = x0 - 3;
			x1 = x1 + 3;
		}
		
		/* modification if the y difference is too small */
		if( Math.abs(yMin - yMax) <= 3 ){
			y0 = y0 - 3;
			y1 = y1 + 3;
		}
		
		/* normal operation */
		if ((p.x > x0) && (p.x < x1) &&
				(p.y > y0) && (p.y < y1)) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public boolean onCorner(Point p) {
				
		int x0 = Math.min(xMin, xMax);
		int x1 = Math.max(xMin, xMax);
		int y0 = Math.min(yMin, yMax);
		int y1 = Math.max(yMin, yMax);
		
		/* modification if the x difference is too small */
		if( Math.abs(xMin - xMax) <= 3 ){
			x0 = x0 - 3;
			x1 = x1 + 3;
		}
		
		/* modification if the y difference is too small */
		if( Math.abs(yMin - yMax) <= 3 ){
			y0 = y0 - 3;
			y1 = y1 + 3;
		}
		
		if ((((p.x >= x0 - 3) && (p.x <= x0 + 3))  ||
		     ((p.x >= x1 - 3) && (p.x <= x1 + 3))) &&
		    (((p.y >= y0 - 3) && (p.y <= y0 + 3))  ||
		     ((p.y >= y1 - 3) && (p.y <= y1 + 3)))) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public void setColor(Color c) {
		color = c;
	}
	
	public Color getColor() {
		return color;
	}
	
}
